package com.jyw.web.controller.system;

import com.jyw.common.core.domain.CommonResult;
import com.jyw.common.core.domain.model.LoginBody;
import com.jyw.common.core.domain.model.MpLoginUser;
import com.jyw.common.core.domain.model.MpLoginUserBinding;
import com.jyw.common.core.domain.model.WechatLoginBody;
import com.jyw.custom.sms.service.ISmsService;
import com.jyw.framework.web.service.SysLoginService;
import com.jyw.system.service.IWeChatAppletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 登录验证
 *
 * @author system
 */
@RestController
@Api(tags = "登录验证")
public class SysLoginController {
    @Resource
    private SysLoginService loginService;

    @Resource
    private IWeChatAppletService weChatAppletService;

    @Resource
    private ISmsService smsService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public CommonResult<String> login(@RequestBody LoginBody loginBody) {
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        return CommonResult.success(null, token);
    }


    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/wechat/login")
    @ApiOperation("小程序登录")
    public CommonResult<String> wechatLogin(@RequestBody WechatLoginBody loginBody) {
        // 生成令牌
        String token = loginService.wechatPhoneLogin(loginBody);
        return CommonResult.success(null, token);
    }


    /**
     * 微信服务号用户登录
     *
     * @return
     */
    @ApiOperation("发送验证码")
    @GetMapping("/code")
    public CommonResult<String> sendCode(String phone) {
        return CommonResult.success("发送成功", smsService.sendCode(phone));
    }

    /**
     * 微信服务号用户登录
     *
     * @return
     */
    @ApiOperation("获取用户openId")
    @GetMapping("/getWxUserInfo")
    public CommonResult<WxOAuth2UserInfo> getWxUserInfo(String code) {
        WxOAuth2UserInfo wxUserInfo=weChatAppletService.getWxUserInfo(code);
        return CommonResult.success("获取成功", wxUserInfo);
    }

    /**
     * 微信服务号用户登录
     *
     * @param bind 请求登录对象
     * @return
     */
    @ApiOperation("微信服务号用户登录")
    @PostMapping("/wechat/mpLogin")
    public CommonResult<String> mpLogin(@RequestBody MpLoginUserBinding bind) {
        String token=loginService.mpLogin(bind);
        return CommonResult.success("登录成功", token);
    }
}
